package com.hdaccp.mybatis.controller;

import com.google.gson.Gson;
import com.hdaccp.mybatis.dao.UserDao;
import com.hdaccp.mybatis.entity.User;
import com.hdaccp.mybatis.test.Test;
import com.hdaccp.mybatis.util.BaseDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/user")
public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter() ;

        BaseDao baseDao = new BaseDao() ;
        SqlSession session = baseDao.getSession() ;

        List<User> list = session.getMapper(UserDao.class).query() ;
        Gson gson = new Gson() ;
        out.print(new Gson().toJson(list)) ;
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
